package com.simba.server.components;

import java.sql.Timestamp;
import com.simba.server.components.data.SorterInfo;
import com.simba.common.utils.FrameUtils;
import com.simba.server.components.data.SortInfo;

public class SortProtocolCreator
{
	private static class SortProtocolManagerHolder
	{
		private static final SortProtocolCreator INSTANCE = new SortProtocolCreator();
	}

	private static final int SLOT_SOFT_LOCK_CONSTANT_SIZE = 17;
	private static final int SLOT_SOFT_LOCK_STATUS_CONSTANT_SIZE = 14;

	public static SortProtocolCreator getInstance()
	{
		return SortProtocolManagerHolder.INSTANCE;
	}


	private SortProtocolCreator()
	{

	}

	public Timestamp createCurrentTimeStamp()
	{
		return new Timestamp(System.currentTimeMillis());
	}

	public byte[] createSortResultBuffer(SorterInfo wheelSorterInfo, SortInfo wheelSortInfo)
	{

		byte[] replyBuffer = null;

		byte[] plcIdBuf = wheelSortInfo.getPlcLdBuf();
		int plcIdLen = 0;
		if (plcIdBuf != null)
		{
			plcIdLen = plcIdBuf.length;
		}
		int slotsNum = wheelSortInfo.getSlotsList().size();
		if (slotsNum == 0)
		{
			wheelSortInfo.getSlotsList().add(99);
			slotsNum += 1;
		}

		int bufferSize = plcIdLen + 1 + 8 + slotsNum;
		replyBuffer = new byte[bufferSize];

		int position = 0;

		byte[] header = SortCommonDefines.REPLY_HEADER;// 2
		System.arraycopy(header, 0, replyBuffer, position, header.length);
		position += header.length;

		byte[] command = {SortCommonDefines.COMMAND_0X04_TEST_SCANNED_BARCODE_REQUEST};// 1
		System.arraycopy(command, 0, replyBuffer, position, command.length);
		position += command.length;

		byte[] sorterIdBuf = new byte[2];
		int wheelSorterId = Integer.parseInt(wheelSorterInfo.getSorterId());
		FrameUtils.getInstance().setShortBigEndian(0, wheelSorterId, sorterIdBuf);
		System.arraycopy(sorterIdBuf, 0, replyBuffer, position, sorterIdBuf.length);
		position += sorterIdBuf.length;

		byte[] plcIdLenBuf = {(byte) plcIdLen};// 1
		System.arraycopy(plcIdLenBuf, 0, replyBuffer, position, plcIdLenBuf.length);
		position += plcIdLenBuf.length;

		if (plcIdLen > 0)
		{
			// plc id len
			System.arraycopy(plcIdBuf, 0, replyBuffer, position, plcIdBuf.length);
			position += plcIdBuf.length;
		}

		byte[] slotsNumBuffer = {(byte) slotsNum};// 1
		System.arraycopy(slotsNumBuffer, 0, replyBuffer, position, slotsNumBuffer.length);
		position += slotsNumBuffer.length;

		// 1*slotsNum
		for (int slot : wheelSortInfo.getSlotsList())
		{
			// byte[] slotBuf = new byte[2];
			// FrameUtils.getInstance().setShortBigEndian(0, slot, slotBuf);
			byte[] slotBuf = {(byte) slot};
			System.arraycopy(slotBuf, 0, replyBuffer, position, slotBuf.length);
			position += slotBuf.length;
		}

		byte[] replayEnd = SortCommonDefines.REPLY_END;// 2
		System.arraycopy(replayEnd, 0, replyBuffer, position, replayEnd.length);
		position += replayEnd.length;

		return replyBuffer;
	}

	public byte[] createSlotPackReply(int sorterId, int slotId, int slotStatus){
		return this.createSlotStatus(SortCommonDefines.COMMAND_0X07_WHEEL_SLOT_PACK,sorterId, slotId, slotStatus);
	}

	public byte[] createSlotStatusLock(int sorterId, int slotId){
		return this.createSlotStatus(SortCommonDefines.COMMAND_0X08_WHEEL_SORT_SLOT_PACK, sorterId, slotId, SortCommonDefines.SLOT_SOFT_LOCK_FLAG);
	}

	public byte[] createSlotStatusUnLock(int sorterId, int slotId){
		return this.createSlotStatus(SortCommonDefines.COMMAND_0X08_WHEEL_SORT_SLOT_PACK,sorterId, slotId, SortCommonDefines.SLOT_SOFT_UNLOCK_FLAG);
	}


	private byte[] createSlotStatus(int commend,int sorterId, int slotId, int slotStatus)
	{
		int bufferLen = 9;

		byte[] replyBuffer = new byte[bufferLen];
		int position = 0;

		byte[] header = SortCommonDefines.REPLY_HEADER;// 2
		System.arraycopy(header, 0, replyBuffer, position, header.length);
		position += header.length;

		byte[] command = { (byte) commend };// 3
		System.arraycopy(command, 0, replyBuffer, position, command.length);
		position += command.length;

		byte[] sorterIdBuf = new byte[2];// 5
		FrameUtils.getInstance().setShortBigEndian(0, sorterId, sorterIdBuf);
		System.arraycopy(sorterIdBuf, 0, replyBuffer, position, sorterIdBuf.length);
		position += sorterIdBuf.length;

		byte[] slotLockStatus = { (byte) slotStatus };// 6
		System.arraycopy(slotLockStatus, 0, replyBuffer, position, slotLockStatus.length);
		position += slotLockStatus.length;

		byte[] slotIdBuf = { (byte) slotId };// 7
		System.arraycopy(slotIdBuf, 0, replyBuffer, position, slotIdBuf.length);
		position += slotIdBuf.length;

		byte[] replyEnd = SortCommonDefines.REPLY_END;// 9
		System.arraycopy(replyEnd, 0, replyBuffer, position, replyEnd.length);

		return replyBuffer;
	}

	public byte[] createSystemTimeToBuffer()
	{
		byte[] timeBuffer = new byte[SortCommonDefines.TIME_STAMP_LENGTH_LONG];
		long now = System.currentTimeMillis();
		FrameUtils.getInstance().setLongBigEndian(0, now, timeBuffer);
		return timeBuffer;
	}

	public void init()
	{

	}
}
